// pages/assetPage/addCoin/addCoin.js
var wallet  = require('../../wallet.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coinType:true,
    coinName:'',
    coinTypeData: [],
    Aflag:'',
    addCoinTypeData:[],
    addCoinFlag:false,
    AddLang:{}
  },
  addCoinType(){
    this.setData({
      coinType:false
    })
  },
  cancelAdd(){
    this.setData({
      coinType:true
    })
  },
  deleteCoin(){
    this.setData({
      coinName:''
    })
  },
  switchChange(eve){
      console.log(eve)
      this.setData({
        addCoinFlag: !this.data.addCoinFlag
      })
      let id = eve.target.dataset.index
      //console.log(id)
      let par = this.data.addCoinTypeData[id]
      //console.log(par)
      let status;
      if(par.status==0){
          status = 'on'
      }else if(par.status == 1){
          status = 'off'
      }
     // let status = this.data.addCoinFlag == true?'off':'on'
      wallet.post('ramification/set-currency-status', { symbol: par.coin_symbol, status: status}, function (res) {
        console.log(res)
      })
  },
  addCoinBtn(e){//添加按钮
    //console.log(e)
    let $this = this
    let id = e.target.dataset.id
    let coinParms = this.data.coinTypeData[id]
    wallet.post('ramification/add', coinParms, function (res) {
      console.log(res)
      $this.data.addCoinTypeData.push(coinParms)
      $this.setData({
        Aflag:id,
        addCoinTypeData: $this.data.addCoinTypeData
      })
      wx.reLaunch({
        url: '../assetPage',
      })
    })
  },
  searchCoin(val,that){
    wx.showNavigationBarLoading() 
    wallet.post('ramification/search', {keyword:val}, function (res) {
      console.log(res)
        wx.hideNavigationBarLoading() 
        that.setData({
          coinTypeData:res.data,
          Aflag:''
        })
    },function(err){
      console.log(err)
      that.setData({
        coinTypeData: []
       
      })
    })
  },
  coinTypeName(e){//搜索事件
   // console.log(e)
    let val = e.detail.value
    /*let coinTypeData = [{ name: 'BOC', coinName: "Bit" }, { name: 'eth', type: 1 }, { name: 'BTC', type: 1 }, { name: 'BEC', type: 1 }, { name: 'BTC', type: 1 }];
    let z = new RegExp(val + '', 'gi')
    coinTypeData = coinTypeData.filter((res, index) => {
      return z.test(res.name);
    })*/
   this.searchCoin && this.searchCoin(val,this)
    this.setData({
      coinName: val
    })
  },
  lookFor(){
    let $this = this;
    wallet.post('ramification/get-currency-list', {}, function (res) {
      console.log(res)
      $this.setData({
        addCoinTypeData: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.lookFor && this.lookFor()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      AddLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']

    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].AddAsset : wx.T.locales['en'].AddAsset
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})